import * as Api from "api";

const state = {
  unSetPassword: false, // 未设置密码，否
  token: localStorage.getItem('so_token') || '',
  user: '',
  is_activated: localStorage.getItem('is_activated') || false,
  webinfo: '',
};

// 获取state的数据
const getters = {
  unSetPassword: (state) => state.unSetPassword,
  user: (state) => state.user,
  token: state => state.token,
  is_activated: state => state.is_activated,
  webinfo: (state) => state.webinfo,
};

// 更新state的数据
const mutations = {
  Webinfo(state, data) {
    state.webinfo = data
  },
  UnSetPassword(state, data) {
    state.unSetPassword = data;
  },
  TOKEN(state, data){
    state.token = data
    localStorage.setItem("so_token", data);
  },
  USERINFO(state, data){
    state.user = data
  },
  LOGOUT(state){
    state.user = '',
    state.token = ''
  },
  is_activated(state, data) {
    state.is_activated = data
  }
};

// 更新state数据的动作
const actions = {
  // SetWebinfo({ commit }, params) {
  //   commit("Webinfo", params);
  // },
  async GetWebinfo({ commit }) {
    const res = await Api.WebInfo()
    if (res.data) {
      commit("Webinfo", res.data);
    }
  },
  UnSetPassword({ commit }, params) {
    commit("UnSetPassword", params);
  },
  async GetUserInfo({ commit }){
    const res = await Api.UserInfo()
    commit('USERINFO', res.data)
  },
  SetUserInfo({ commit }, data){
    commit('USERINFO', data)
  },
  Token({ commit }, data) {
    localStorage.setItem("is_activated", data.is_activated);
    commit('TOKEN', data.token)
    commit('is_activated', data.is_activated)
  },
  Logout({ commit }) {
    localStorage.removeItem("so_token");
    localStorage.removeItem("is_activated");
    commit('is_activated', false)
    commit('LOGOUT')
  },
  Set_activated({ commit }, data) {
    localStorage.setItem("is_activated", data);
    commit('is_activated', data)
  }
};

export default { state, getters, mutations, actions };
